<template>
  <el-menu default-active="预约申请" class="el-menu-demo" mode="horizontal" @select="handleSelect">
    <div style="position: relative; top: 0px; height: 21px;font-weight:bold; font-size: 20px">实验预约</div>
    <el-menu-item index="" @click="goBack">
      <i class="el-icon-back"></i>
      <span>返回主页</span>
    </el-menu-item>
    <el-menu-item index="预约申请">
      <i class="el-icon-edit-outline"></i>
      <span>预约申请</span>
    </el-menu-item>
    <el-menu-item index="时间选择">
      <i class="el-icon-time"></i>
      <span>时间选择</span>
    </el-menu-item>
    <el-menu-item index="回收站">
      <i class="el-icon-delete"></i>
      <span>回收站</span>
    </el-menu-item>
  </el-menu>

  <center>
  <div style="max-width: 1000px; margin-bottom: 20px; margin-top: 20px">
    <el-input v-model="search" placeholder="填入项目名称进行搜索" ></el-input>
  </div>

  <div style="max-width: 1000px" v-if="mode==='预约申请'" v-loading.fullscreen.lock="loading">
    <el-collapse model="1" accordion>
      <el-collapse-item
          v-for="item in tryIt"
          :key="item.id"
          :title="item.id + ' - ' + item.name"
          :name="item.id + ' - ' + item.name"
          v-show="searchPicker(item.name)">
        <div style="margin-left: 50px">
          <el-table :data="item.tableData1" style="width: 100%">
            <el-table-column prop="projectno" label="项目编号"/>
            <el-table-column prop="projectname" label="项目名称"/>
            <el-table-column prop="piname" label="独立PI"/>
            <el-table-column prop="asname" label="实验助理"/>
            <el-table-column fixed="right" label="操作">
              <template #default={row}>
                <el-button type="text" :e="row"
                           @click="handleExpMesEdit(item)"
                           size="small">编辑</el-button>
              </template>
            </el-table-column>
          </el-table>
          <el-collapse model="1" accordion>
            <el-collapse-item title="实验步骤" name="1">
              <div style="margin-left: 50px">
                <el-collapse model="1" accordion v-for="item1 in item.stepData" :key="item1.id">
                  <el-collapse-item :title=" '第' + item1.tableData2[0].steppriority + '步：' + item1.name"
                                    :name="item1.id+'-'+item1.tableData2[0].steppriority + '-' + item1.name">
                    <div style="margin-left: 50px">
                      <el-table :data="item1.tableData2" style="width: 100%">
                        <el-table-column prop="steppriority" label="步骤优先等级"/>
                        <el-table-column prop="steepneedtime" label="步骤所需时间"/>
<!--                        <el-table-column fixed="right" label="操作" width="100">-->
<!--                          <template #default={row}>-->
<!--                            <el-button type="text"-->
<!--                                @click="handleStepEdit(item.id, item1.id, item1.tableData2[0], row)"-->
<!--                                size="small">编辑</el-button>-->
<!--                          </template>-->
<!--                        </el-table-column>-->
                      </el-table>
                      <el-collapse model="1" accordion v-for="item2 in item1.unitData" :key="item2.id">
                        <el-collapse-item
                            :title=" '第' + item2.priority + '单元：' + item2.name"
                            :name="item2.priority + '-' + item2.name">
                          <div style="margin-left: 50px">
                            <el-table :data="item2.tableData3" style="width: 100%">
                              <el-table-column prop="moretype" label="附加资源类型"/>
                              <el-table-column prop="moreres" label="附加资源名称"/>
                              <el-table-column prop="moreresnum" label="附加资源数量"/>
<!--                              <el-table-column fixed="right" label="操作">-->
<!--                                <template #default={row}>-->
<!--                                  <div v-if="row.must">（必须）</div>-->
<!--                                  <div v-else>-->
<!--                                    <el-button type="text"-->
<!--                                               @click="handleUnitResAdd(-->
<!--                                                   item.id, item1.id, item1.tableData2[0].steppriority,-->
<!--                                                   item2.id, item2.priority, row, 1)"-->
<!--                                               size="small">增</el-button>-->
<!--                                    <el-button type="text"-->
<!--                                               @click="handleUnitResAdd(-->
<!--                                                   item.id, item1.id, item1.tableData2[0].steppriority,-->
<!--                                                   item2.id, item2.priority, row, -1)"-->
<!--                                               size="small">减</el-button>-->
<!--                                    <el-button type="text"-->
<!--                                               @click="handleUnitResDel(-->
<!--                                                   item.id, item1.id, item1.tableData2[0].steppriority,-->
<!--                                                   item2.id, item2.priority, row)"-->
<!--                                               size="small">删除</el-button>-->
<!--                                  </div>-->
<!--                                </template>-->
<!--                              </el-table-column>-->
                            </el-table>
<!--                            <el-button type="info" style="width: 100%" icon="el-icon-plus"-->
<!--                                       @click="handleUnitResInit(-->
<!--                                           item.id, item1.id, item1.tableData2[0].steppriority,-->
<!--                                           item2.id, item2.priority, item2.tableData3)">-->
<!--                              附加资源-->
<!--                            </el-button>-->
                          </div>
                        </el-collapse-item>
                      </el-collapse>
<!--                      <el-button type="danger" plain style="width: 100%" icon="el-icon-delete"-->
<!--                                 @click="handleStepDel(item.stepData, item1)">-->
<!--                        删除步骤-->
<!--                      </el-button>-->
                    </div>
                  </el-collapse-item>
                </el-collapse>
<!--                <el-button type="success" style="width: 100%" icon="el-icon-plus">添加步骤</el-button>-->
              </div>
            </el-collapse-item>
          </el-collapse>
          <el-button v-if="item.submiting" style="width: 100%; border-width: 0px" :loading="true">审核中</el-button>
          <div v-else>
            <el-button type="info" plain style="width: 47%" icon="el-icon-check"
                       @click="changeState(item, '10019990', '提交成功！请于时间选择界面选择时间', '提交失败，请检查服务器连接')">提交申请</el-button>
            <el-button type="danger" plain style="width: 47%" icon="el-icon-delete"
                       @click="changeState(item, '10010013', '撤销成功！可于回收站界面查看', '撤销失败，请检查服务器连接')">撤销申请</el-button>
          </div>
        </div>
      </el-collapse-item>
      <el-button type="primary" style="width: 100%" icon="el-icon-plus" @click="checkAddExp">添加实验申请</el-button>
    </el-collapse>
  </div>

  <div style="max-width: 1000px" v-if="mode==='时间选择'">
    <el-collapse model="1" accordion>
      <el-collapse-item
          v-for="item in tryIt"
          :key="item.id"
          :title="item.id + ' - ' + item.name"
          :name="item.id + ' - ' + item.name"
          v-show="searchPicker(item.name)">
        <div style="margin-left: 50px">
          <el-table :data="item.tableData1" style="width: 100%">
            <el-table-column prop="projectno" label="项目编号"/>
            <el-table-column prop="projectname" label="项目名称"/>
            <el-table-column prop="piname" label="独立PI"/>
            <el-table-column prop="asname" label="实验助理"/>
          </el-table>
          <el-collapse model="1" accordion>
            <el-collapse-item title="实验步骤" name="1">
              <div style="margin-left: 50px">
                <el-collapse model="1" accordion v-for="item1 in item.stepData" :key="item1.id">
                  <el-collapse-item :title=" '第' + item1.tableData2[0].steppriority + '步：' + item1.name"
                                    :name="item1.id+'-'+item1.tableData2[0].steppriority + '-' + item1.name">
                    <div style="margin-left: 50px">
                      <el-table :data="item1.tableData2" style="width: 100%">
                        <el-table-column prop="steppriority" label="步骤优先等级"/>
                        <el-table-column prop="steepneedtime" label="所需时间（小时）"/>
                      </el-table>
                      <el-collapse model="1" accordion v-for="item2 in item1.unitData" :key="item2.id">
                        <el-collapse-item
                            :title=" '第' + item2.priority + '单元：' + item2.name"
                            :name="item2.priority + '-' + item2.name">
                          <div style="margin-left: 50px">
                            <el-table :data="item2.tableData3" style="width: 100%">
                              <el-table-column prop="moretype" label="附加资源类型"/>
                              <el-table-column prop="moreres" label="附加资源名称"/>
                              <el-table-column prop="moreresnum" label="附加资源数量"/>
                              <el-table-column fixed="right" label="是否必须">
                                <template #default={row}>
                                  <div v-if="row.must">必须</div>
                                  <div v-else>额外</div>
                                </template>
                              </el-table-column>
                            </el-table>
                            <br>
                            <el-table :data="item2.tableData4" style="width: 100%; margin-bottom: 20px">
                              <h5 style="float: left">可开始时段</h5>
                              <el-table-column prop="startTime" label="可预约时间段开始时刻"/>
                              <el-table-column prop="endTime" label="可预约时间段开始时刻"/>
                            </el-table>
                            <div style="width: 47%;float: left;font-size: 14px">预计时长（小时）：</div>
                            <el-input style="width: 47%;margin-bottom: 10px" v-model="item2.hour" readonly="true"
                                      @click="changeTime(item.id, item1.id, item1.tableData2[0].steppriority, item2)"/>
                            <br>
                            <div style="width: 47%;float: left;font-size: 14px">开始时间：</div>
                            <el-date-picker value-format="yyyy-MM-dd HH:mm:ss" style="width: 47%" v-model="item2.select_time" type="datetime" placeholder="选择日期时间"/>
                          </div>
                        </el-collapse-item>
                      </el-collapse>
                    </div>
                  </el-collapse-item>
                </el-collapse>
              </div>
            </el-collapse-item>
          </el-collapse>
          <el-button v-if="item.submiting" style="width: 100%; border-width: 0px" :loading="true">审核中</el-button>
          <div v-else>
            <el-button type="info" plain style="width: 48%" icon="el-icon-check" @click="submitTime(item)">提交时间</el-button>
            <el-button type="danger" plain style="width: 48%" icon="el-icon-delete"
                       @click="changeState(item, '10010013', '撤销成功！可于回收站界面查看', '撤销失败，请检查服务器连接')">撤销申请</el-button>
          </div>
        </div>
      </el-collapse-item>
    </el-collapse>
  </div>

  <div style="max-width: 1000px" v-if="mode==='回收站'">
    <el-collapse model="1" accordion>
      <el-collapse-item
          v-for="item in tryIt"
          :key="item.id"
          :title="item.id + ' - ' + item.name"
          :name="item.id + ' - ' + item.name"
          v-show="searchPicker(item.name)">
        <div style="margin-left: 50px">
          <el-table :data="item.tableData1" style="width: 100%">
            <el-table-column prop="projectno" label="项目编号"/>
            <el-table-column prop="projectname" label="项目名称"/>
            <el-table-column prop="piname" label="独立PI"/>
            <el-table-column prop="asname" label="实验助理"/>
          </el-table>
          <el-collapse model="1" accordion>
            <el-collapse-item title="实验步骤" name="1">
              <div style="margin-left: 50px">
                <el-collapse model="1" accordion v-for="item1 in item.stepData" :key="item1.id">
                  <el-collapse-item :title=" '第' + item1.tableData2[0].steppriority + '步：' + item1.name"
                                    :name="item1.id+'-'+item1.tableData2[0].steppriority + '-' + item1.name">
                    <div style="margin-left: 50px">
                      <el-table :data="item1.tableData2" style="width: 100%">
                        <el-table-column prop="steppriority" label="步骤优先等级"/>
                        <el-table-column prop="steepneedtime" label="步骤所需时间"/>
                      </el-table>
                      <el-collapse model="1" accordion v-for="item2 in item1.unitData" :key="item2.id">
                        <el-collapse-item
                            :title=" '第' + item2.priority + '单元：' + item2.name"
                            :name="item2.priority + '-' + item2.name">
                          <div style="margin-left: 50px">
                            <el-table :data="item2.tableData3" style="width: 100%">
                              <el-table-column prop="moretype" label="附加资源类型"/>
                              <el-table-column prop="moreres" label="附加资源名称"/>
                              <el-table-column prop="moreresnum" label="附加资源数量"/>
                              <el-table-column fixed="right" label="是否必须">
                                <template #default={row}>
                                  <div v-if="row.must">必须</div>
                                  <div v-else>额外</div>
                                </template>
                              </el-table-column>
                            </el-table>
                          </div>
                        </el-collapse-item>
                      </el-collapse>
                    </div>
                  </el-collapse-item>
                </el-collapse>
              </div>
            </el-collapse-item>
          </el-collapse>
          <el-button v-if="item.submiting" style="width: 100%; border-width: 0px" :loading="true">审核中</el-button>
          <div v-else>
            <el-button type="info" plain style="width: 48%" icon="el-icon-upload2"
                       @click="changeState(item, '10010010', '重新添加成功！请于预约申请界面确认', '重新添加失败，请检查服务器连接')">重新申请</el-button>
            <el-button type="danger" plain style="width: 48%" icon="el-icon-delete"
                       @click="delExp(item, '删除成功！', '删除失败，请检查服务器连接')">删除记录</el-button></div>
        </div>
      </el-collapse-item>
    </el-collapse>
  </div>
  </center>

  <el-dialog title="基本项目信息修改"
             v-model="visible.handleExpMesEdit"
             @close ="visible.handleExpMesEdit=false">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="200px" label-position="left" style="width: 700px">
      <el-form-item label="项目编号:" prop="projectno">
        <div style="float:left">{{ruleForm.projectno}}</div>
      </el-form-item>
      <el-form-item label="项目名称:" prop="projectname">
        <div style="float:left">{{ruleForm.projectname}}</div>
      </el-form-item>
      <el-form-item label="实验编号:" prop="experimentno">
        <div style="float:left">{{ruleForm.experimentno}}</div>
      </el-form-item>
      <el-form-item label="实验名称:" prop="experimentname">
        <el-input v-model="ruleForm.experimentname"></el-input>
      </el-form-item>
      <el-form-item label="独立PI:" prop="PIno">
        <el-select v-model="ruleForm.PIno" @click="newPIASListPI()" placeholder="请选择独立PI编号" style="width: 500px">
          <el-option
              v-for="item in ruleForm.PIASListPI"
              :key="item.PIuser_mix"
              :value="item.PIuser_mix">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="实验助理:" prop="assistantno">
        <el-select v-model="ruleForm.assistantno" @click="newPIASListAS()" placeholder="请选择实验助理编号" style="width: 500px">
          <el-option
              v-for="item in ruleForm.PIASListAS"
              :key="item.ASuser_mix"
              :value="item.ASuser_mix">
          </el-option>
        </el-select>
      </el-form-item>
      <el-button type="primary" @click="addexperimentbutton" style="width: 100%">修改</el-button>
    </el-form>
  </el-dialog>

  <el-dialog title="功能单元时长修改"
             v-model="visible.handleUnitTimeEdit.visible"
             @close ="visible.handleUnitTimeEdit.visible=false">
    <el-form :model="visible.handleUnitTimeEdit" :rules="rules" ref="ruleForm" label-width="200px" label-position="left" style="width: 700px">
      <el-form-item label="时长:">
        <el-input-number v-model="visible.handleUnitTimeEdit.hour" :precision="2" :step="1" :min="0"></el-input-number>
      </el-form-item>
      <el-button type="primary" @click="changeTimeAns" style="width: 100%">修改</el-button>
    </el-form>
  </el-dialog>


</template>



<script>
import GLOBAL from "@/utils/global_variable"
import {ElMessage} from "element-plus";
import {get, post} from "@/utils/http"

export default {
  name: "BookingPage",
  data(){
    return{
      dialogVisible: false,
      search: "",
      uid: null,
      typeId: null,
      uname: null,
      time1: null,
      mode: "预约申请",
      rules: {
        projectno:[
          { required: true, message: '请输入项目编号', trigger: 'blur' }
        ],
        timeSelect:[
          { required: true, message: '请选择时间', trigger: 'blur' }
        ],
        projectname:[
          { required: true, message: '请输入项目名称', trigger: 'blur' }
        ],
        experimentno:[
          { required: true, message: '请输入实验编号', trigger: 'blur' }
        ],
        experimentname:[
          { required: true, message: '请输入实验名称', trigger: 'blur' }
        ],
        PIno:[
          { required: true, message: '请输入独立PI编号', trigger: 'blur' }
        ],
        PIname:[
          { required: true, message: '请输入独立PI名称', trigger: 'blur' }
        ],
        assistantno:[
          { required: true, message: '请输入实验助理编号', trigger: 'blur' }
        ],
        assistantname:[
          { required: true, message: '请输入实验助理名称', trigger: 'blur' }
        ],
        delprojectno:[
          { required: true, message: '请输入项目编号', trigger: 'blur' }
        ],
        delexperimentno:[
          { required: true, message: '请输入实验编号', trigger: 'blur' }
        ],
        experimentstepno:[
          { required: true, message: '请输入唯一标识', trigger: 'blur' }
        ],
        stepno:[
          { required: true, message: '请输入步骤编号', trigger: 'blur' }
        ],
        stepname:[
          { required: true, message: '请输入步骤名称', trigger: 'blur' }
        ],
        steppriority:[
          { required: true, message: '请输入优先等级', trigger: 'blur' }
        ],
        steptime:[
          { required: true, message: '请输入步骤时长', trigger: 'blur' }
        ],
        delsteono:[
          { required: true, message: '请输入步骤编号', trigger: 'blur' }
        ],
        unitexpstepno:[
          { required: true, message: '请输入唯一标识', trigger: 'blur' }
        ],
        unitno:[
          { required: true, message: '请输入单元编号', trigger: 'blur' }
        ],
        unitname:[
          { required: true, message: '请输入单元名称', trigger: 'blur' }
        ],
        type:[
          { required: true, message: '请输入资源种类', trigger: 'blur' }
        ],
        resno:[
          { required: true, message: '请输入资源编号', trigger: 'blur' }
        ],
        resname:[
          { required: true, message: '请输入资源名称', trigger: 'blur' }
        ],
        resnum:[
          { required: true, message: '请输入资源数量', trigger: 'blur' }
        ],
        delunitexpstepno:[
          { required: true, message: '请输入唯一标识', trigger: 'blur' }
        ],
        delunitno:[
          { required: true, message: '请输入单元编号', trigger: 'blur' }
        ],
        delrestype:[
          { required: true, message: '请输入资源种类', trigger: 'blur' }
        ],
        delresno:[
          { required: true, message: '请输入资源编号', trigger: 'blur' }
        ],
        unitstepno:[
          { required: true, message: '请输入资源编号', trigger: 'blur' }
        ],
      },
      loading: false,
      visible: {
        handleExpMesEdit: false,
        handleUnitTimeEdit: {
          hour: 0,
          visible: false
        },
      },
      dialogData: [],
      ruleForm: {
        projectno: '',
        projectname: '',
        experimentno: '',
        experimentname: '',
        PIno: '',
        PIname: '',
        assistantno: '',
        assistantname: '',
        delprojectno: '',
        delexperimentno: '',

        // experimentstepno: '',
        stepno: '',
        stepname: '',
        steppriority: '',
        steptime: '',
        delsteono: '',
        delstep_id1: '',
        delpriority1: '',
        delstep_time_hour1: '',

        unitexpstepno: '',
        unitstepno: '',
        unitno: '',
        unitname: '',
        type: '',
        resno: '',
        resname: '',
        resnum: '',
        delunitexpstepno: '',
        delunitno: '',
        delrestype: '',
        delresno: '',

        readonly1: false,

        unitList:[],

        restypeList: ['SAMPLE','MATERIAL','ANIMAL'],
        yangpinList: [],
        haocaiList: [],
        yiqiList: [],
        nowuseList: [],
        nowuseList2: [],

        stepnoList: [],
        experimentstepList: [],

        PIASList: [],
        PIASListPI: [],
        PIASListAS: [],
      },
      tryIt: [{
        id: '00011',
        name: '生猪养殖研究 - 离线示例',
        submiting: false,
        tableData1: [{
          projectno: 'P0001',
          projectname: '生猪 Project',
          piname: '王小乐',
          asname: '张小仙'
        }],
        stepData:[{
          id: 'S01',
          name: '提取DNA',
          tableData2:[{
            steppriority: 1,
            steepneedtime: 2
          }],
          unitData:[{
            id: '111',
            name: '抽血',
            priority: 1,
            hour: 1,
            select_time: "2021-07-20 13:57:07",
            tableData3:[
              {
                moreid: '003',
                moretype: '动物',
                moreres: '猪',
                moreresnum: '3',
                must: false,
              }, {
                moreid: '006',
                moretype: '耗材',
                moreres: '试管',
                moreresnum: '1',
                must: true,
              }, {
                moreid: '006',
                moretype: '耗材',
                moreres: '试管',
                moreresnum: '2',
                must: false,
              },
            ],
            tableData4: [
              {startTime:"2021-07-20 13:57:07", endTime:"2021-07-20 18:57:07"},
            ]
          }]
        }]
      }],
    }
  },
  created() {
    this.uid = GLOBAL.params.uid;
    this.typeId = GLOBAL.params.typeId;
    this.uname = GLOBAL.params.uname;
    this.searchExp()
    this.getstep();
    this.getyangpin();
    this.gethaocai();
    this.getyiqi();
    this.getunitlist();
    this.getPIASlist();
  },
  methods: {
    goBack() {
      this.$router.push({
        name: "MainPage",
        params: {
          uid: this.uid
        }
      })
    },
    handleSelect(key) {
      this.mode = key
      console.log(key)
      this.loading = true
      this.searchExp()
      this.loading = false
    },
    timeFormat(javaDate) {
      return javaDate.split(".")[0].replace("T", " ")
    },
    searchPicker(na) {
      let str = ['',...this.search,''].join('.*');
      let reg = new RegExp(str) //正则
      return reg.test(na)
    },
    timeJSFormat(time){
      const date = new Date(time);
      const year = date.getFullYear(),
          month = date.getMonth() + 1,//月份是从0开始的
          day = date.getDate(),
          hour = date.getHours(),
          min = date.getMinutes(),
          sec = date.getSeconds();
      return year + '-' +
          (month < 10 ? '0' + month : month) + '-' +
          (day < 10 ? '0' + day : day) + ' ' +
          (hour < 10 ? '0' + hour : hour) + ':' +
          (min < 10 ? '0' + min : min) + ':' +
          (sec < 10 ? '0' + sec : sec);
    },
    searchExp() {
      let that = this;
      get('/expbooking/get_exp_mes', {
        uid: GLOBAL.params.uid,
        utypeid: GLOBAL.params.typeId
      }).then(function (res) {
        if (res.code === 1) {
          let first = true
          for (let ep_N in res.data) {
            if (first) {
              that.tryIt = []
              first = false
            }
            let rep = res.data[ep_N]
            console.log(rep.pre_progress_id)
            if (rep.pre_progress_id!=='10010010' && that.mode==='预约申请'){ continue }
            if ((rep.pre_progress_id<'10019990' || rep.pre_progress_id>='19999990') && that.mode==='时间选择'){ continue }
            if (rep.pre_progress_id!=='10010013' && that.mode==='回收站'){ continue }
            let ep = {
              id: rep.experiment_id,
              name: rep.experiment_name,
              submiting: false,
              tableData1: [{
                projectno: rep.project_id,
                projectname: rep.project_name,
                piname: rep.independent_pi_name,
                asname: rep.lab_assistant_name,
                piid: rep.independent_pi_id,
                asid: rep.lab_assistant_id
              }],
              stepData:[]
            }
            that.tryIt.push(ep)
            that.getUnitData(ep)
          }
        } else {
          ElMessage.warning({
            showClose: true,
            message: "查询失败，请检查网络状态！",
            type: "warning",
            center: true
          })
        }
      })
    },
    getUnitData(ep) {
      let that = this;
      get('/expbooking/get_step_iden', {
        exp_id: ep.id
      }).then(function (res) {
        if (res.code === 1) {
          for (let da in res.data) {
            let dai = res.data[da]
            let st = {
              id: dai.step_id,
              name: dai.step_name,
              tableData2: [{
                steppriority: dai.step_priority,
                steepneedtime: dai.step_time_hour
              }],
              unitData: []
            }
            ep.stepData.push(st)
            that.setEpUnit(st, ep.id, dai.step_id, dai.step_priority)
          }
        } else {
          ElMessage.warning({
            showClose: true,
            message: "查询失败，请检查网络状态！",
            type: "warning",
            center: true
          })
        }
      })
    },
    setEpUnit(st, exp_id, step_id, step_pr){
      let that = this;
      get('/expbooking/get_unit_iden_mes', {
        exp_id: exp_id,
        step_id: step_id,
        step_pr: step_pr
      }).then(function (res) {
        if (res.code === 1) {
          for (let en in res.data) {
            let ei = res.data[en]
            let unit = {
              id: ei.id,
              name: ei.name,
              priority: ei.priority,
              hour: null,
              select_time: null,
              tableData3:[],
              tableData4:[]
            }
            st.unitData.push(unit)
            that.setUnitRes(exp_id, step_id, step_pr, ei.id, unit)
            that.setUnitHour(exp_id, step_id, step_pr, ei.id, ei.priority, unit)
          }
        } else {
          ElMessage.warning({
            showClose: true,
            message: "查询失败，请检查网络状态！",
            type: "warning",
            center: true
          })
        }
      })
    },
    setUnitRes(exp_id, step_id, step_pr, unit_id, unit){
      get('/expbooking/get_unit_all_res', {
        exp_id: exp_id,
        step_id: step_id,
        step_pr: step_pr,
        unit_id: unit_id
      }).then(function (res) {
        if (res.code === 1) {
          unit.tableData3 = []
          for (let unie in res.data){
            let uni = res.data[unie]
            unit.tableData3.push({
              moreid: uni.res_id,
              moretype: uni.res_type,
              moreres: uni.res_name,
              moreresnum: uni.res_num,
              must: uni.must
            })
          }
        } else {
          ElMessage.warning({
            showClose: true,
            message: "查询失败，请检查网络状态！",
            type: "warning",
            center: true
          })
        }
      })
    },
    setUnitHour(exp_id, step_id, step_pr, unit_id, unit_pr, unit){
      let that = this
      get('/expbooking/get_unit_time', {
        exp_id: exp_id,
        step_id: step_id,
        step_pr: step_pr,
        unit_id: unit_id,
        unit_pr: unit_pr
      }).then(function (res) {
        if (res.code === 1) {
          unit.hour = res.data.unit_hour
          that.setUnitTimes(exp_id, step_id, step_pr, unit_id, unit_pr, unit)
        } else {
          ElMessage.warning({
            showClose: true,
            message: "查询失败，请检查网络状态！",
            type: "warning",
            center: true
          })
        }
      })
    },
    setUnitTimes(exp_id, step_id, step_pr, unit_id, unit_pr, unit){
      let that = this
      get('/expbookupd/query_unit_time', {
        exp_id: exp_id,
        step_id: step_id,
        step_pr: step_pr,
        unit_id: unit_id,
        unit_pr: unit_pr
      }).then(function (res) {
        if (res.code === 1) {
          unit.tableData4 = []
          for (let sei in res.data){
            let se = res.data[sei]
            unit.tableData4.push({
              startTime: that.timeFormat(se.startTime),
              endTime: that.timeFormat(se.endTime)
            })
          }
        } else {
          ElMessage.warning({
            showClose: true,
            message: "查询失败，请检查网络状态！",
            type: "warning",
            center: true
          })
        }
      })
    },
    getstep(){
      let that = this;
      get('/stepnonamejs', {
      }).then(function (res) {
        if (res.code === 1) {
          that.ruleForm.stepnoList = [];
          for(var i=0;i<res.data.length;i++){
            that.ruleForm.stepnoList.push({id: res.data[i].stepno, value: res.data[i].stepname});
          }
        } else {
          ElMessage.warning({
            showClose: true,
            message: "步骤信息加载失败",
            type: "warning",
            center: true
          })
        }
      });
    },
    getyangpin(){
      let that = this;
      get('/getyangpinjs', {
      }).then(function (res) {
        if (res.code === 1) {
          that.ruleForm.yangpinList = [];
          for(var i=0;i<res.data.length;i++){
            that.ruleForm.yangpinList.push({id: res.data[i].yangpinid, value: res.data[i].yangpinname});
          }
        } else {
          ElMessage.warning({
            showClose: true,
            message: "样品信息加载失败",
            type: "warning",
            center: true
          })
        }
      });
    },
    gethaocai(){
      let that = this;
      get('/gethaocaijs', {
      }).then(function (res) {
        if (res.code === 1) {
          that.ruleForm.haocaiList = [];
          for(var i=0;i<res.data.length;i++){
            that.ruleForm.haocaiList.push({id: res.data[i].yangpinid, value: res.data[i].yangpinname});
          }
        } else {
          ElMessage.warning({
            showClose: true,
            message: "耗材信息加载失败",
            type: "warning",
            center: true
          })
        }
      });
    },
    getyiqi(){
      let that = this;
      get('/getyiqijs', {
      }).then(function (res) {
        if (res.code === 1) {
          that.ruleForm.yiqiList = [];
          for(var i=0;i<res.data.length;i++){
            that.ruleForm.yiqiList.push({id: res.data[i].yangpinid, value: res.data[i].yangpinname});
          }
        } else {
          ElMessage.warning({
            showClose: true,
            message: "仪器信息加载失败",
            type: "warning",
            center: true
          })
        }
      });
    },
    getexperimentstep(){
      let that = this;
      get('/getexperimentstepjs', {
        experimentno: that.ruleForm.experimentno,
      }).then(function (res) {
        if (res.code === 1) {
          that.ruleForm.experimentstepList = [];
          for(var i=0;i<res.data.length;i++){
            that.ruleForm.experimentstepList.push({
              experiment_step_id1: res.data[i].experimentstepno,
              step_id1: res.data[i].stepno,
              priority1: res.data[i].steppriority,
              step_time_hour1: res.data[i].steptime });
          }
        } else {
          ElMessage.warning({
            showClose: true,
            message: "实验数据更新失败",
            type: "warning",
            center: true
          })
        }
      });
    },
    getunitlist(){
      let that = this;
      get('/getunitlistjs', {
      }).then(function (res) {
        if (res.code === 1) {
          that.ruleForm.unitList = [];
          for(var i=0;i<res.data.length;i++){
            that.ruleForm.unitList.push({
              unitList_id: res.data[i].yangpinid,
              unitList_name: res.data[i].yangpinname});
          }
        } else {
          ElMessage.warning({
            showClose: true,
            message: "单元数据更新失败",
            type: "warning",
            center: true
          })
        }
      });
    },
    getPIASlist(){
      let that = this;
      get('/piasjs', {
      }).then(function (res) {
        if (res.code === 1) {
          that.ruleForm.PIASList = [];
          for(var i=0;i<res.data.length;i++){
            that.ruleForm.PIASList.push({
              PIASuser_id: res.data[i].user_id,
              PIASuser_name: res.data[i].user_name,
              PIASuser_type: res.data[i].user_type,
              PIASproject_id: res.data[i].project_id});
          }
        } else {
          ElMessage.warning({
            showClose: true,
            message: "人员数据更新失败",
            type: "warning",
            center: true
          })
        }
      });
    },
    handleExpMesEdit(item) {
      this.ruleForm.projectno = item.tableData1[0].projectno
      this.ruleForm.projectname = item.tableData1[0].projectname
      this.ruleForm.experimentno = item.id
      this.ruleForm.experimentname = item.name
      this.ruleForm.PIno = item.tableData1[0].piid + ' : ' + item.tableData1[0].piname
      this.ruleForm.PIname = item.tableData1[0].piname
      this.ruleForm.assistantno = item.tableData1[0].asid + ' : ' + item.tableData1[0].asname
      this.ruleForm.assistantname = item.tableData1[0].asname
      this.visible.handleExpMesEdit = true
    },
    newPIASListPI(){
      this.ruleForm.PIASListPI=[];
      for(var i=0;i<this.ruleForm.PIASList.length;i++){
        let us = this.ruleForm.PIASList[i]
        if(us.PIASuser_type === 3 && us.PIASproject_id === this.ruleForm.projectno){
          this.ruleForm.PIASListPI.push({
            PIuser_id: us.PIASuser_id,
            PIuser_name: us.PIASuser_name,
            PIuser_mix: us.PIASuser_id + ' : ' + us.PIASuser_name
          })
        }
      }
    },
    newPIASListAS(){
      this.ruleForm.PIASListAS=[];
      for(var i=0;i<this.ruleForm.PIASList.length;i++){
        let us = this.ruleForm.PIASList[i]
        if(us.PIASuser_type === 4 && us.PIASproject_id === this.ruleForm.projectno){
          this.ruleForm.PIASListAS.push({
            ASuser_id: us.PIASuser_id,
            ASuser_name: us.PIASuser_name,
            ASuser_mix: us.PIASuser_id + ' : ' + us.PIASuser_name
          })
        }
      }
    },
    elmes(is, mes){
      if (is){
        ElMessage.success({
          showClose: true,
          message: mes,
          type: "success",
          center: true
        })
      }
      else{
        ElMessage.warning({
          showClose: true,
          message: mes,
          type: "warning",
          center: true
        })
      }
    },
    addexperimentbutton() {
      this.visible.handleExpMesEdit = false
      this.loading = true
      let that = this;
      get('/expbookupd/upd_exp_mes', {
        exp_id: that.ruleForm.experimentno,
        exp_name: that.ruleForm.experimentname,
        pi_id: that.ruleForm.PIno.split(' : ')[0],
        as_id: that.ruleForm.assistantno.split(' : ')[0]
      }).then(function (res) {
        if (res.code === 1) {
          that.searchExp()
          that.loading = false
          that.elmes(true, "修改成功")
        } else {
          that.loading = false
          that.elmes(false, "修改失败")
        }
      });
    },
    changeState(item, pgr_id, sms, fms) {
      item.loading = true
      let that = this;
      get('/expbooking/change_exp_state', {
        exp_id: item.id,
        pgr_id: pgr_id
      }).then(function (res) {
        if (res.code === 1) {
          that.searchExp()
          item.loading = false
          that.elmes(true, sms)
        } else {
          item.loading = false
          that.elmes(false, fms)
        }
      });
    },
    delExp(item, sms, fms) {
      item.loading = true
      let that = this;
      get('/delexperimentbuttonjs', {
        experimentno: item.id
      }).then(function (res) {
        if (res.code === 1) {
          that.searchExp()
          item.loading = false
          that.elmes(true, sms)
        } else {
          item.loading = false
          that.elmes(false, fms)
        }
      });
    },
    changeTime(exp_id, step_id, step_pr, unit) {
      this.dialogData = [exp_id, step_id, step_pr, unit]
      this.visible.handleUnitTimeEdit.visible = true
      this.visible.handleUnitTimeEdit.hour = unit.hour
    },
    changeTimeAns() {
      this.visible.handleUnitTimeEdit.visible = false
      let exp_id = this.dialogData[0]
      let step_id = this.dialogData[1]
      let step_pr = this.dialogData[2]
      let unit = this.dialogData[3]
      this.loading = true
      let that = this
      get('/expbooking/set_unit_time', {
        exp_id: exp_id,
        step_id: step_id,
        step_pr: step_pr,
        unit_id: unit.id,
        unit_pr: unit.priority,
        unit_hour: that.visible.handleUnitTimeEdit.hour
      }).then(function (res) {
        if (res.code === 1) {
          unit.hour = that.visible.handleUnitTimeEdit.hour
          that.setUnitTimes(exp_id, step_id, step_pr, unit.id, unit.priority, unit)
          that.loading = false
          that.elmes(true, "修改成功！")
        } else {
          that.loading = false
          that.elmes(false, "修改失败！")
        }
      });
    },
    submitTime(Exp) {
      this.loading = true
      let output = []
      if (this.checkTime(Exp)){
        for (let stepi in Exp.stepData) {
          let step = Exp.stepData[stepi]
          for (let uniti in step.unitData){
            let unit = step.unitData[uniti]
            output.push({
              exp_id: Exp.id,
              step_id: step.id,
              step_pr: step.tableData2[0].steppriority,
              unit_id: unit.id,
              unit_pr: unit.priority,
              unit_time: this.timeJSFormat(unit.select_time)
            })
          }
        }
        let that = this
        post('/expbookupd/submit_unit_time', output).then(function (res) {
          that.loading = false
          if (res.code === 1) {
            that.elmes(true, "提交成功！")
            this.searchExp()
          } else {
            that.elmes(false, "提交失败！")
          }
        });
      }
    },
    checkTime(Exp) {
      let isCorrect = true
      for (let stepi in Exp.stepData) {
        let step = Exp.stepData[stepi]
        step.stepTime = ["9999-99-99 99:99:99", "0000-00-00 00:00:00"]
        let alltime = 0
        for (let uniti in step.unitData){
          let unit = step.unitData[uniti]
          alltime += unit.hour
          if (unit.select_time == null){
            this.elmes(false,'第' + step.tableData2[0].steppriority + '步：' + step.name+"的"+
              '第' + unit.priority + '单元：' + unit.name+"的时间未选择完全！")
            return false
          }
          let unitTime = this.timeJSFormat(unit.select_time)
          if (step.stepTime[0] > unitTime) { step.stepTime[0] = unitTime }
          if (step.stepTime[1] < unitTime) { step.stepTime[1] = unitTime } // TODO 可优化
          let inIt = false
          for(let timei in unit.tableData4) {
            let time = unit.tableData4[timei]
            if (time.startTime <= unitTime && time.endTime >= unitTime){
              console.log(time.startTime,unitTime,time.endTime)
              inIt = true
              break
            }
          }
          if (inIt === false) {
            this.elmes(false,'第' + step.tableData2[0].steppriority + '步：' + step.name+"的"+
                '第' + unit.priority + '单元：' + unit.name+"的时间未在指定范围内！")
            return false
          }
          for (let unitj in step.unitData) {
            let unito = step.unitData[unitj]
            let unitTimeo = this.timeJSFormat(unito.select_time)
            if (unitTimeo > unitTime && unit.priority > unito.priority){
              this.elmes(false,'第' + step.tableData2[0].steppriority + '步：' + step.name+"的"+
                  '第' + unit.priority + '单元：' + unit.name+"和"+
                  '第' + unito.priority + '单元：' + unito.name+"优先级冲突！")
              return false
            }
          }
        }
        if (alltime > step.tableData2[0].steepneedtime) {
          this.elmes(false,'第' + step.tableData2[0].steppriority + "步：时长超过预期！")
          return false
        }
      }
      for (let stepi in Exp.stepData) {
        let step = Exp.stepData[stepi]
        step.stepTime = ["9999-99-99 99:99:99", "0000-00-00 00:00:00"]
        for (let stepj in Exp.stepData){
          let stepo = Exp.stepData[stepj]
          if (stepo.stepTime[0] < step.stepTime[1] && step.tableData2[0].steppriority < stepo.tableData2[0].steppriority){
            this.elmes(false,'第' + step.tableData2[0].steppriority + '步：' + step.name+"和"+
                '第' + stepo.tableData2[0].steppriority + '步：' + stepo.name+"优先级冲突！")
            return false
          }
        }
      }
      return isCorrect
    },
    checkAddExp(){
      let that = this;
      that.$router.push({
        name: "AddExperiment",
      });
    },
  }
}
</script>

<style scoped>

</style>